package com.ezjie.ielts.db;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ezjie.ielts.model.EwordMeaning;
import com.ezjie.ielts.util.DownloadUtil;
import com.ezjie.ielts.util.LogUtils;

public class WordMeaningDao {

	private static final String TABLE_NAME = "e_word_meaning";
	private static final String COLUMN_MEANING_ID = "meaning_id";
	private static final String COLUMN_WORD_ID = "word_id";
	private static final String COLUMN_POS = "pos";
	private static final String COLUMN_LANG = "lang";
	private static final String COLUMN_TEXT = "text";
	private static final String COLUMN_CREATE_TIME = "create_time";
	private static final String COLUMN_UPDATE_TIME = "update_time";
	private static final String COLUMN_IS_ACTIVE = "is_active";
	private SQLiteDatabase database;

	public WordMeaningDao(Context context) {
		DownloadUtil.initCacheDir(context);
		String databaseFilename = DownloadUtil.WORD_CACHE_PATH + "/" + DownloadUtil.WORD_DATABASE_FILENAME;
		File file = new File(databaseFilename);
		if (!file.exists()) {
			// 如果数据库文件不存在 就再拷贝一份
			DownloadUtil.getInstance().copyWordDB2SD(context);
		}

		if (file.exists()) {
			database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);
		}
	}

	/**
	 * 根据单词id 查找单词
	 * 
	 * @param word_id
	 * @return
	 */
	public List<EwordMeaning> findByWordId(int word_id) {
		List<EwordMeaning> meanings = new ArrayList<EwordMeaning>();
		if (database == null) {
			return meanings;
		}
		Cursor cursor = database.query(TABLE_NAME, null, COLUMN_WORD_ID + "=? and " + COLUMN_IS_ACTIVE + "=?",
				new String[] { word_id + "", 1 + "" }, null, null, null);
		while (cursor.moveToNext()) {
			EwordMeaning ewordMeaning = new EwordMeaning();
			ewordMeaning.setWord_id(cursor.getInt(cursor.getColumnIndex(COLUMN_WORD_ID)));
			ewordMeaning.setLang(cursor.getString(cursor.getColumnIndex(COLUMN_LANG)));
			ewordMeaning.setMeaning_id(cursor.getInt(cursor.getColumnIndex(COLUMN_MEANING_ID)));
			ewordMeaning.setPos(cursor.getString(cursor.getColumnIndex(COLUMN_POS)));
			ewordMeaning.setText(cursor.getString(cursor.getColumnIndex(COLUMN_TEXT)));
			ewordMeaning.setCreate_time(cursor.getString(cursor.getColumnIndex(COLUMN_CREATE_TIME)));
			ewordMeaning.setUpdate_time(cursor.getString(cursor.getColumnIndex(COLUMN_UPDATE_TIME)));
			ewordMeaning.setIs_active(cursor.getInt(cursor.getColumnIndex(COLUMN_IS_ACTIVE)));
			meanings.add(ewordMeaning);
		}
		cursor.close();
		return meanings;
	}

	public EwordMeaning findByMeaningId(int meaning_id) {
		EwordMeaning ewordMeaning = new EwordMeaning();
		if (database == null) {
			return ewordMeaning;
		}
		Cursor cursor = database.query(TABLE_NAME, null, COLUMN_MEANING_ID + "=?", new String[] { meaning_id + "" },
				null, null, null);
		while (cursor.moveToNext()) {
			ewordMeaning.setMeaning_id(cursor.getInt(cursor.getColumnIndex(COLUMN_MEANING_ID)));
			ewordMeaning.setWord_id(cursor.getInt(cursor.getColumnIndex(COLUMN_WORD_ID)));
			ewordMeaning.setPos(cursor.getString(cursor.getColumnIndex(COLUMN_POS)));
			ewordMeaning.setLang(cursor.getString(cursor.getColumnIndex(COLUMN_LANG)));
			ewordMeaning.setText(cursor.getString(cursor.getColumnIndex(COLUMN_TEXT)));
			ewordMeaning.setCreate_time(cursor.getString(cursor.getColumnIndex(COLUMN_CREATE_TIME)));
			ewordMeaning.setUpdate_time(cursor.getString(cursor.getColumnIndex(COLUMN_UPDATE_TIME)));
			ewordMeaning.setIs_active(cursor.getInt(cursor.getColumnIndex(COLUMN_IS_ACTIVE)));

		}
		cursor.close();
		return ewordMeaning;
	}

	public EwordMeaning findByMeaningIdOrNull(int meaning_id) {
		EwordMeaning ewordMeaning = null;
		if (database == null) {
			return ewordMeaning;
		}
		Cursor cursor = database.query(TABLE_NAME, null, COLUMN_MEANING_ID + "=?", new String[] { meaning_id + "" },
				null, null, null);
		while (cursor.moveToNext()) {
			ewordMeaning = new EwordMeaning();
			ewordMeaning.setMeaning_id(cursor.getInt(cursor.getColumnIndex(COLUMN_MEANING_ID)));
			ewordMeaning.setWord_id(cursor.getInt(cursor.getColumnIndex(COLUMN_WORD_ID)));
			ewordMeaning.setPos(cursor.getString(cursor.getColumnIndex(COLUMN_POS)));
			ewordMeaning.setLang(cursor.getString(cursor.getColumnIndex(COLUMN_LANG)));
			ewordMeaning.setText(cursor.getString(cursor.getColumnIndex(COLUMN_TEXT)));
			ewordMeaning.setCreate_time(cursor.getString(cursor.getColumnIndex(COLUMN_CREATE_TIME)));
			ewordMeaning.setUpdate_time(cursor.getString(cursor.getColumnIndex(COLUMN_UPDATE_TIME)));
			ewordMeaning.setIs_active(cursor.getInt(cursor.getColumnIndex(COLUMN_IS_ACTIVE)));

		}
		cursor.close();
		return ewordMeaning;
	}

	public void insert(EwordMeaning eword) {
		if (database != null && eword != null) {
			ContentValues values = new ContentValues();

			values.put(COLUMN_MEANING_ID, eword.getMeaning_id());
			values.put(COLUMN_WORD_ID, eword.getWord_id());
			values.put(COLUMN_POS, eword.getPos());
			values.put(COLUMN_LANG, eword.getLang());
			values.put(COLUMN_TEXT, eword.getText());
			values.put(COLUMN_CREATE_TIME, eword.getCreate_time());
			values.put(COLUMN_UPDATE_TIME, eword.getUpdate_time());
			values.put(COLUMN_IS_ACTIVE, eword.getIs_active());
			long insert = database.insert(TABLE_NAME, null, values);
			if (insert != -1) {
				LogUtils.d("插入wordMeaning成功");
			} else {
				LogUtils.d("插入wordMeaning失败");
			}
		}
	}

	public void update(EwordMeaning eword) {
		if (database != null && eword != null) {
			ContentValues values = new ContentValues();
			values.put(COLUMN_MEANING_ID, eword.getMeaning_id());
			values.put(COLUMN_WORD_ID, eword.getWord_id());
			values.put(COLUMN_POS, eword.getPos());
			values.put(COLUMN_LANG, eword.getLang());
			values.put(COLUMN_TEXT, eword.getText());
			values.put(COLUMN_CREATE_TIME, eword.getCreate_time());
			values.put(COLUMN_UPDATE_TIME, eword.getUpdate_time());
			values.put(COLUMN_IS_ACTIVE, eword.getIs_active());
			int update = database.update(TABLE_NAME, values, COLUMN_MEANING_ID + "=?",
					new String[] { eword.getMeaning_id() + "" });
		}
	}

	public void updateList(List<EwordMeaning> list) {
		for (EwordMeaning ewordMeaning : list) {
			EwordMeaning findByMeaningId = findByMeaningIdOrNull(ewordMeaning.getMeaning_id());
			if (findByMeaningId == null) {
				insert(ewordMeaning);
			} else {
				update(ewordMeaning);
			}
		}
	}
}
